/**
 * 创建星空背景
 * @param {HTMLElement} container
 */
export function createStarfield(container) {
    if (!container) return;
    const starCount = 200;
    for (let i = 0; i < starCount; i++) {
        const star = document.createElement('div');
        star.classList.add('star');
        const size = Math.random() * 2 + 0.5;
        star.style.width = `${size}px`;
        star.style.height = `${size}px`;
        star.style.left = `${Math.random() * 100}%`;
        star.style.top = `${Math.random() * 100}%`;
        star.style.animation = `twinkle ${Math.random() * 3 + 2}s infinite ease-in-out`;
        star.style.animationDelay = `${Math.random() * 3}s`;
        container.appendChild(star);
    }
}

/**
 * 为角色黑洞创建粒子效果
 * @param {HTMLElement} container - 粒子要添加到的容器元素
 * @param {string} color - 粒子的基础颜色 (十六进制)
 */
export function createParticles(container, color) {
    if (!container) return;
    container.innerHTML = ''; // 清除旧粒子
    const particleCount = 15;

    for (let i = 0; i < particleCount; i++) {
        const wrapper = document.createElement('div');
        wrapper.className = 'particle-wrapper';

        const radius = Math.random() * (container.offsetWidth / 1.5) + (container.offsetWidth / 4);
        const angle = Math.random() * Math.PI * 2;

        const x = Math.cos(angle) * radius;
        const y = Math.sin(angle) * radius;

        wrapper.style.transform = `translate(${x}px, ${y}px)`;

        const particle = document.createElement('div');
        particle.className = 'particle-inner';

        const size = Math.random() * 3 + 1;
        particle.style.width = `${size}px`;
        particle.style.height = `${size}px`;
        const opacity = Math.random() * 0.5 + 0.3;
        particle.style.backgroundColor = `${color}${Math.floor(opacity * 255).toString(16).padStart(2, '0')}`;
        
        const spinDuration = Math.random() * 20 + 10;
        const pulseDuration = Math.random() * 2 + 1;
        particle.style.animation = `spin-in-place ${spinDuration}s linear infinite, pulse ${pulseDuration}s ease-in-out infinite alternate`;

        wrapper.appendChild(particle);
        container.appendChild(wrapper);
    }
}

/**
 * 调整十六进制颜色的亮度
 * @param {string} color - 基础颜色
 * @param {number} percent - 调整百分比 (-100 to 100)
 * @returns {string} 调整后的新颜色
 */
export function shadeColor(color, percent) {
    let R = parseInt(color.substring(1, 3), 16);
    let G = parseInt(color.substring(3, 5), 16);
    let B = parseInt(color.substring(5, 7), 16);
    R = parseInt(R * (100 + percent) / 100);
    G = parseInt(G * (100 + percent) / 100);
    B = parseInt(B * (100 + percent) / 100);
    R = (R < 255) ? R : 255;
    G = (G < 255) ? G : 255;
    B = (B < 255) ? B : 255;
    const RR = ((R.toString(16).length === 1) ? "0" + R.toString(16) : R.toString(16));
    const GG = ((G.toString(16).length === 1) ? "0" + G.toString(16) : G.toString(16));
    const BB = ((B.toString(16).length === 1) ? "0" + B.toString(16) : B.toString(16));
    return "#" + RR + GG + BB;
}